package cn.enilu.flash.mobile.controller;

import cn.enilu.flash.bean.entity.shop.Goods;
import cn.enilu.flash.bean.entity.shop.Label;
import cn.enilu.flash.bean.entity.shop.LabelGoods;
import cn.enilu.flash.bean.vo.front.Rets;
import cn.enilu.flash.bean.vo.query.SearchFilter;
import cn.enilu.flash.service.shop.GoodsService;
import cn.enilu.flash.service.shop.LabelGoodsService;
import cn.enilu.flash.service.shop.LabelService;
import cn.enilu.flash.utils.Lists;
import cn.enilu.flash.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 〈功能概述〉<br>
 *
 * @className: LabelController
 * @package: cn.enilu.flash.mobile.controller
 * @author: wuchaohong
 * @date: 2020/8/13 15:24
 */

@RestController
@RequestMapping("/label")
public class LabelController {
    @Autowired
    private LabelService labelService;
    @Autowired
    private LabelGoodsService labelGoodsService;
    @Autowired
    private GoodsService goodsService;

    /**
     *获取所有首页推荐标签
     */
    @RequestMapping(value = "/getRecommendLabel",method = RequestMethod.GET)
    public Object getRecommendLabel(){
        Sort sort = Sort.by(Sort.Direction.DESC, "modifyTime");
        List<Label> labelList = labelService.queryAll(SearchFilter.build("recommend",1),sort);
        labelList.forEach(l->{
            List<LabelGoods> labelGoodsList = labelGoodsService.findByLabelId(l.getId());
            labelGoodsList.forEach(i->{
                List<SearchFilter> filters = Lists.newArrayList(
                        SearchFilter.build("id", i.getGoodsId()),
                        SearchFilter.build("isDelete", false),
                        SearchFilter.build("isOnSale", true),
                        SearchFilter.build("isAudit", 1)
                );
                List<Goods> goodsList = goodsService.queryAll(filters,sort);
                if(null != goodsList && goodsList.size()>0){
                    i.setGoods(goodsList.get(0));
                }
            });
            l.setLabelGoodsList(labelGoodsList);
        });
        return Rets.success(labelList);
    }
}
